package com.mysoft.gfyx.common.util;

import com.mysoft.gfyx.common.dto.SystemQueryUserBusinessUnit.Condition;
import com.mysoft.gfyx.common.dto.SystemQueryUserBusinessUnit.Params;
import com.mysoft.gfyx.common.dto.RequestDto;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class QueryParamUtil {
    public static RequestDto BuildRequestDto(String[] fields, String[][] wheres, int Page, int PageSize) {

        RequestDto requestDto = new RequestDto();
        // 构建查询字段
        if (fields != null) {
            //requestDto.setSelectFields(fields);
        }
        requestDto.setPageIndex(Page);
        requestDto.setPageSize(PageSize);
        // 构建查询条件
        Params params = new Params();
        params.setOperator("and");
        List<Condition> conditions = new ArrayList<>();
        for (int i = 0; i < wheres.length; i++) {
            // 只有满足 {eq, field_name, field_val} 3个才能算
            if (wheres[i].length == 3) {
                Condition condition = new Condition();
                condition.setOperator(wheres[i][0]);
                condition.setFieldName(wheres[i][1]);
                if (Objects.equals(wheres[i][0], "in") || Objects.equals(wheres[i][0], "not in")) {
                    //当使用in查询时，value为逗号分隔的字符串
                    condition.setValue(wheres[i][2].split(","));
                } else {
                    condition.setValue(wheres[i][2]);
                }
                conditions.add(condition);
            }
        }
        params.setConditions(conditions);
        requestDto.setParameters(params);

        return requestDto;
    }
}
